package de.lighti.starcraft;

import java.util.Collection;
import java.util.Vector;

import de.lighti.starcraft.model.StarcraftBot;
import de.lighti.starcraft.model.StarcraftMap;

public class StarcraftBotStats {
    private final StarcraftBot bot;
    private final Vector<Object[]> wins;

    public StarcraftBotStats( StarcraftBot bot ) {
        super();
        this.bot = bot;
        wins = new Vector<Object[]>();
    }

    public StarcraftBot getBot() {
        return bot;
    }

    public int getTotalWins() {
        return getTotalWins( null );
    }

    public int getTotalWins( Collection<StarcraftMap> onlyOnMaps ) {
        int ret = 0;
        for (final Object[] o : wins) {
            if (onlyOnMaps == null || onlyOnMaps.contains( o[1] )) {
                ret++;
            }
        }

        return ret;
    }

    public int hasWonOver( StarcraftBotStats col ) {
        return hasWonOver( col, null );
    }

    public int hasWonOver( StarcraftBotStats col, Collection<StarcraftMap> onlyOnMaps ) {
        int ret = 0;
        for (final Object[] win : wins) {
            final StarcraftBot b = (StarcraftBot) win[0];
            if (b.equals( col.getBot() ) && (onlyOnMaps == null || onlyOnMaps.contains( win[1] ))) {
                ret++;
            }
        }

        return ret;
    }

    public float winrate( StarcraftBotStats col, Collection<StarcraftMap> onlyOnMaps ) {
        final int thisWins = hasWonOver( col, onlyOnMaps );
        final int otherWins = col.hasWonOver( this, onlyOnMaps );

        final float totalGames = thisWins + otherWins;

        return thisWins / totalGames;
    }

    public void wonOver( StarcraftBot two, StarcraftMap map ) {
        wins.add( new Object[] { two, map } );
    }
}
